import {observable, action, runInAction, } from "mobx";
import { NetInfo } from "react-native";

import {API_CONFIG} from "../config/api";

class WeiboAPI {
	@observable isConnected = null;
	//微博的数据；
	@observable list = [];
	//来福岛笑话的数据；
	@observable funList = [];
	@observable newsList = [];
	//下拉刷新；
	@observable refreshing = {
		refresh: false,
	}
	//请求微博数据；
	@action async fetchWeibo(iteminfo) {
		// alert(API_CONFIG.network);
		const ret = await fetch(API_CONFIG[iteminfo], { method: "GET" })
		.then((response) => {
			return response.json();
		})
		.then((jsonData) => {
			return jsonData;
		})
		.catch((error) => {
			console.log(error)
		})

		if(ret) {
			runInAction("request success", () => {
				this.list = ret.showapi_res_body.pagebean["contentlist"];
			}) 
		}
	}
	//请求funny数据；
	@action async fetchFunny() {
		const ret = await fetch(API_CONFIG.funUrl, { method: "GET" })
		.then((response) => {
			return response.json();
		})
		.then((jsonData) => {
			return jsonData;
		})
		.catch((error) => {
			console.log(error);
		})
		if(ret) {
			runInAction("fetch success", () => {
				this.funList = ret.showapi_res_body.list;
			})
		}
	}
	//请求news数据；
	@action async fetchNews() {
		const ret = await fetch(API_CONFIG.newsUrl, { method: "GET" })
		.then((response) => {
			return response.json();
		})
		.then((jsonData) => {
			return jsonData;
		})
		.catch((error) => {
			return console.log(error);
		})
		if(ret) {
			runInAction("resquest success", () => {
				this.newsList = ret.showapi_res_body.pagebean["contentlist"];
			})
		}
	}
	// @action async getNetInfo() {
	// 	const ret = await  NetInfo.isConnected.fetch().then( isConnected => {
	//      return isConnected;
	//     });
 //    	runInAction("fetch success", () => {
 //    		this.isConnected = ret;
	// 		if(UseAPI.isConnected === false) {
	// 			ToastAndroid.show('网络连接不可用!', ToastAndroid.SHORT);
	// 		}
 //    	})
	// }
}

export default new WeiboAPI();